<script>
    // 一、通过reduce和filter方法将以下一维数组转换成树状图
    let data = [
        { id: 1, name: 'a', pid: 0 },
        { id: 2, name: 'aa', pid: 0 },
        { id: 3, name: 'aaa', pid: 1 },
        { id: 4, name: 'aaaa', pid: 2 },
        { id: 5, name: 'aaaaa', pid: 2 },
        { id: 6, name: 'aaaaaa', pid: 2 },
        { id: 7, name: 'aaaaaaa', pid: 1 },
        { id: 8, name: 'aaaaaaaa', pid: 3 },
        { id: 9, name: 'aaaaaaaaa', pid: 3 },
    ]
    function findParent(id, data) {
        return data.find(item => item.id === id);
    }
    function findChildren(pid, data) {
        return data.filter(item => item.pid === pid);
    }
    function buildTree(data) {
        let roots = []
        data.reduce((tree, item) => {
            if (item.pic === 0) {
                roots.push(item)
            }
            else {
                let parent = findParent(item.pid, data);
            }
            if (parent) {
                if (!parent.children) parent.children = [];
                parent.children.push(item);
            }

        })
    }
    console.log(data);
</script>